package main

import (
	"fmt"
	"gocolly/model"
	"gocolly/utils"
	"log"
	"os"
	"slices"
	"strconv"
	"strings"
	"time"
)

// 学校图片，设施介绍
var schoolImageAndDevice = "https://static-data.gaokao.cn/www/2.0/school/image/%s.json?a=www.gaokao.cn"

// 学校排名
var schoolAllRank = "https://static-data.gaokao.cn/www/2.0/school/%s/rank.json?a=www.gaokao.cn"

// 学校详细信息
var schoolManyDetails = "https://static-data.gaokao.cn/www/2.0/school/%s/info.json?a=www.gaokao.cn"

// 奖学金
var award = "https://static-data.gaokao.cn/www/2.0/school/award/%s.json?a=www.gaokao.cn"

var res []byte
var count = 0
var total = model.TotalRows()

// 获取所有已经拉取过的ID
var pullSchoolIds = pulledSchoolIds()

// 检测已经拉过的数据
func pulledSchoolIds() []string {

	dirs, err := os.ReadDir("./static/school_info/award/")
	if err != nil {
		log.Fatalln("读取目录失败：", err)
	}

	var pulledIds []string

	for _, dir := range dirs {
		files := strings.Split(dir.Name(), ".")
		name := files[0]
		pulledIds = append(pulledIds, name)
	}
	return pulledIds
}

func dealRow(row *model.SchoolNew) {
	count++
	schoolID := strings.TrimSpace(row.SchoolID)

	if slices.Contains(pullSchoolIds, schoolID) {
		fmt.Println("跳过:", schoolID)
	} else {
		fmt.Println("拉取：", schoolID)
		ImageAndDevice := fmt.Sprintf(schoolImageAndDevice, schoolID)
		res = downSchoolJSON(ImageAndDevice)
		os.WriteFile("./static/school_info/image_devices/"+schoolID+".json", res, os.ModePerm)

		AllRank := fmt.Sprintf(schoolAllRank, strings.TrimSpace(row.SchoolID))
		res = downSchoolJSON(AllRank)
		os.WriteFile("./static/school_info/all_rank/"+schoolID+".json", res, os.ModePerm)

		ManyDetails := fmt.Sprintf(schoolManyDetails, strings.TrimSpace(row.SchoolID))
		res = downSchoolJSON(ManyDetails)
		os.WriteFile("./static/school_info/many_details/"+schoolID+".json", res, os.ModePerm)

		Award := fmt.Sprintf(award, strings.TrimSpace(row.SchoolID))
		res = downSchoolJSON(Award)
		os.WriteFile("./static/school_info/award/"+schoolID+".json", res, os.ModePerm)

		fmt.Println("第" + strconv.Itoa(count) + "行，总共：" + strconv.Itoa(int(total)) + "行")
		time.Sleep(time.Second * 10)
	}
	//

}

func main() {
	model.RowsDeal(dealRow)

}

func downSchoolImage() {

	//下载大学图片
	imageUrl := "https://static-data.gaokao.cn/upload/school/202119/1618803138_3461_thumb.jpg"
	savePath := "./static/school_info/school_image/江苏大学/"
	title := "校门"
	utils.DownloadImage(imageUrl, savePath, title)
}

func downSchoolJSON(url string) []byte {
	headers := map[string]string{
		"Accept": "application/json, text/plain, */*",
		//"Accept-Encoding":    "gzip, deflate, br, zstd",
		"Accept-Language":    "zh-CN,zh;q=0.9",
		"Content-Length":     "202",
		"Content-Type":       "application/json",
		"Origin":             "https://www.gaokao.cn",
		"Priority":           "u=1, i",
		"Referer":            "https://www.gaokao.cn/",
		"Sec-Ch-Ua":          `Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"`,
		"Sec-Ch-Ua-Mobile":   `?0`,
		"Sec-Ch-Ua-Platform": "Windows",
		"Sec-Fetch-Dest":     "empty",
		"Sec-Fetch-Mode":     "cors",
		"Sec-Fetch-Site":     "cross-site",
		"User-Agent":         "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
	}
	req := &utils.Request{
		URL:     url,
		Method:  "GET",
		Timeout: 10 * time.Second,
		Headers: headers,
		//ProxyUrl: "http://127.0.0.1:7890", //本地的vpn代理
		ProxyUrl: "", //本地的vpn代理
	}
	return req.Do()
}
